<!DOCTYPE html>
<html lang="en" data-ng-app="App">

<head>
    <meta charset="UTF-8">

    <title>云图轨道交通数据管理系统-登录</title>
    <link rel="stylesheet" href="http://cdn.staticfile.org/font-awesome/4.2.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="/authenticateResources/style.css">
</head>

<body data-ng-controller="homeCtrl">
    <div id="login-container">
        <div id="sgidi">
            用户登录
        </div>
        <div class="divider"></div>
        <div class="form-wrapper" id="form-wrapper">
            <form id="form" action="/login" method="post" enctype="application/x-www-form-urlencoded">
                <div class="input-section">
                    <label for="username">
                        用户名 <i class="fa fa-user"></i>
                    </label>
                    <input id="username" type="text" data-ng-model="userName" name="username" required placeholder="用户名" autofocus="autofocus">
                </div>
                <div class="input-section">
                    <label for="password">
                        密码 <i class="fa fa-lock"></i>
                    </label>
                    <input type="password" name="password" data-ng-model="password" required placeholder="输入你的密码" data-ng-keyup="$event.keyCode === 13 && login($event)">
                </div>
                <div class="input-section">
                    <label for="username">
                        &nbsp;
                    </label>
                    <a class="submit-btn" data-ng-click="login($event)">
                        登录
                    </a>
                    <input type="hidden" id="login" value="登录">
                    <a href="/register1" class="register-btn" style="margin-left:10px;">申请账号</a>
                    <a href="/findPassword" class="link-btn" style="margin-left:10px;color:#4E8BDB;">找回密码</a>
                </div>
            </form>
        </div>

    </div>
    <footer>©2014-2015 上海岩土工程勘察设计研究院有限公司-地理信息所版权所有</footer>
</body>
<script src="http://cdn.staticfile.org/angular.js/1.3.0-beta.13/angular.min.js"></script>
<script>
    var app = angular.module('App', []);
app.controller('homeCtrl', ['$scope', '$timeout', '$http', '$window', function($scope, $timeout, $http, $window) {
   
    var redirect = window.location.search.replace(/^\?/,'');
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    var username = readCookie('username') ? decodeURI(readCookie('username')) : "";
    document.getElementById('username').setAttribute('value', username);

    var shake = (function() {

        var shakeRun = false;

        function shake() {
            if (shakeRun) return;
            var loginBtn = document.getElementById('form-wrapper');
            shakeRun = true;
            angular.element(loginBtn).addClass('shake');
            $timeout(function() {
                shakeRun = false;
                angular.element(loginBtn).removeClass('shake');
            }, 800);
        };
        return shake;
    })();


    $scope.login = function(event) {
        event.stopPropagation();
        event.preventDefault();
        
        if (!$scope.userName || !$scope.password) {
            shake();
        } else {
            $http({
                method: 'post',
                url: '/login/matchCode',
                data: {
                    userName: $scope.userName,
                    password: $scope.password,
                    redirect: redirect
                }
            }).success(function(data) {
                if (data === 'true') {
                    //
                    var path = '/'+decodeURI(redirect);
                    window.location = path;
                    // $window.location=path;
                    // document.getElementById('form').submit();
                } else {
                    shake();
                }
            });
        }
    };


}]);

</script>

</html>